package com.dimeina.dao.impl;

import java.sql.SQLException;
import java.util.ArrayList;

import com.dimeina.dao.UserDao;
import com.dimeina.util.BaseDAO;
import com.dimeina.vo.User;


public class UserDaoImpl extends BaseDAO implements UserDao {
	public UserDaoImpl() {
		super();
	}
	/**
	 * @author Michael
	 * 注册功能_系列方法
	 */
	// 查询所有注册用户的账号
	public ArrayList<String> findAllUser() {
		String sql = "SELECT `username` FROM users";
		ArrayList<String> loginNameList = new ArrayList<String>();
		rs = executeQuery(sql, new Object[]{});
		// 获取所有用户账号
		try {
			if (rs != null) {
				while (rs.next()) {
					String username = rs.getString("username");
					loginNameList.add(username);
				}
			}
		} catch (SQLException e) {
			System.out.println("获取数据集合有误！");
			e.printStackTrace();
		} catch (Exception e) {
			System.out.println("获取数据集合其他异常！");
			e.printStackTrace();
		} finally {
			closeAll(rs, pstmt, conn);
		}
		return loginNameList;
	}

	// 检查用户名是否注册
	// 返回true  没有重复的用户名    可以注册
	public boolean checkUsername(String username) {
		ArrayList<String> loginNameList = new ArrayList<String>();
		loginNameList = findAllUser();
		boolean judge = true;
		if (loginNameList != null && loginNameList.size() > 0) {
			for (String name : loginNameList) {
				if (name.equals(username)) {
					judge = false;
				}
			}
		}
		if (judge == true) {
			return true;
		} else {
			return false;
		}
	}

	// 插入新用户
	public int insertUser(User newsUser) {
		// 返回插入的记录行号
		int result = 0;
		try {
			String sql = "INSERT INTO `users` (`username`,`password`) VALUES (?,?)";
			result = executeUpdate(sql, newsUser.getUserName(), newsUser.getUserPassword());
		} catch (Exception e) {
			System.out.println("注册用户信息异常！！！");
			e.printStackTrace();
		} finally {
			closeAll(rs, pstmt, conn);
		}
		return result;
	}
	
	
	/**
	 * @author Michael
	 * 登录功能_系列方法
	 */
	// 获取所有用户的账号和密码
	public ArrayList<User> findAllUnameUpwd() {
		String sql = "SELECT `username`,`password` FROM users";
		ArrayList<User> loginUserList = new ArrayList<User>();
		rs = executeQuery(sql, new Object[]{});
		// 获取所有用户账号
		try {
			if (rs != null) {
				while (rs.next()) {
					String username = rs.getString("username");
					String password = rs.getString("password");
					User newsUser = new User(username,password);
					loginUserList.add(newsUser);
				}
			}
		} catch (SQLException e) {
			System.out.println("获取数据集合有误！");
			e.printStackTrace();
		} catch (Exception e) {
			System.out.println("获取数据集合其他异常！");
			e.printStackTrace();
		} finally {
			closeAll(rs, pstmt, conn);
		}
		return loginUserList;
	}
	
	// 检查用户账号和密码是否正确
	// 返回true 此用户名 和 密码 都正确    可以登录
	public boolean checkUnameUpwd(String username, String password) {
		ArrayList<User> loginUserList = new ArrayList<User>();
		loginUserList = findAllUnameUpwd();
		boolean judge = false;
		if (loginUserList != null && loginUserList.size() > 0) {
			for (User user : loginUserList) {
				if (user.getUserName().equals(username)) {
					if (user.getUserPassword().equals(password)) {
						judge = true;
					}
				}
			}
		}
		if (judge == true) {
			return true;
		} else {
			return false;
		}
	}

	
	
}
